package com.gqj.system.hk;

import NetSDKDemo.HCNetSDK;
import com.sun.jna.Native;
import org.json.JSONException;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;


@Component
public class AcsMain {

    static HCNetSDK hCNetSDK = null;
    static int lUserID = -1;//用户句柄
    static int iCharEncodeType = 0;//设备字符集



    /**
     * 根据不同操作系统选择不同的库文件和库路径
     *
     * @return
     */
    public static boolean createSDKInstance() {
        if (hCNetSDK == null) {
            synchronized (HCNetSDK.class) {
                String strDllPath = "";
                try {
                    //System.setProperty("jna.debug_load", "true");
                    //win系统加载库路径
                    strDllPath = System.getProperty("user.dir") + "\\lib\\HCNetSDK.dll";
                    hCNetSDK = (HCNetSDK) Native.loadLibrary(strDllPath, HCNetSDK.class);
                    hCNetSDK.NET_DVR_Init();
                    //开启SDK日志打印
                    boolean i= hCNetSDK.NET_DVR_SetLogToFile(3, "./sdklog", false);
                    System.out.println("海康威视-sdk初始化成功");
                } catch (Exception ex) {
                    System.out.println("loadLibrary: " + strDllPath + " Error: " + ex.getMessage());
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 设备登录
     *
     * @param ipadress IP地址
     * @param user     用户名
     * @param psw      密码
     * @param port     端口，默认8000
     */
    public static void login_V40(String ipadress, String user, String psw, short port) {
        //注册
        HCNetSDK.NET_DVR_USER_LOGIN_INFO m_strLoginInfo = new HCNetSDK.NET_DVR_USER_LOGIN_INFO();//设备登录信息
        String m_sDeviceIP = ipadress;//设备ip地址
        m_strLoginInfo.sDeviceAddress = new byte[HCNetSDK.NET_DVR_DEV_ADDRESS_MAX_LEN];
        System.arraycopy(m_sDeviceIP.getBytes(), 0, m_strLoginInfo.sDeviceAddress, 0, m_sDeviceIP.length());
        String m_sUsername = user;//设备用户名
        m_strLoginInfo.sUserName = new byte[HCNetSDK.NET_DVR_LOGIN_USERNAME_MAX_LEN];
        System.arraycopy(m_sUsername.getBytes(), 0, m_strLoginInfo.sUserName, 0, m_sUsername.length());
        String m_sPassword = psw;//设备密码
        m_strLoginInfo.sPassword = new byte[HCNetSDK.NET_DVR_LOGIN_PASSWD_MAX_LEN];
        System.arraycopy(m_sPassword.getBytes(), 0, m_strLoginInfo.sPassword, 0, m_sPassword.length());
        m_strLoginInfo.wPort = port; //sdk端口
        m_strLoginInfo.bUseAsynLogin = false; //是否异步登录：0- 否，1- 是
        m_strLoginInfo.write();
        HCNetSDK.NET_DVR_DEVICEINFO_V40 m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V40();//设备信息
        lUserID = hCNetSDK.NET_DVR_Login_V40(m_strLoginInfo, m_strDeviceInfo);
        if (lUserID == -1) {
            System.out.println("海康卫士登录失败，错误码为" + hCNetSDK.NET_DVR_GetLastError());
            return;
        } else {
            System.out.println("海康卫士登录成功！");
            m_strDeviceInfo.read();
            iCharEncodeType = m_strDeviceInfo.byCharEncodeType;
            return;

        }
    }


    /**
     * 登出操作
     */
    public static void logout() {
        if(hCNetSDK!=null) {
            /**登出和清理，释放SDK资源*/
            if (lUserID >= 0) {
                if (!hCNetSDK.NET_DVR_Logout(lUserID)) {
                    System.out.println("设备注销失败，错误码：" + hCNetSDK.NET_DVR_GetLastError());
                    return;
                }
                System.out.println("设备注销成功！！！");
            }
        }
    }
}